package com.yfuxdeer.youchat.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yfuxdeer.youchat.R;
import com.yfuxdeer.youchat.pojo.MomentItem;
import com.yfuxdeer.youchat.ui.RoundImageView;
import com.yfuxdeer.youchat.utils.ImageUtil;
import com.yfuxdeer.youchat.utils.DateUtil;
import com.yfuxdeer.youchat.utils.Store;

import java.util.List;

public class MomentListAdapter extends BaseAdapter {

    private final static String TAG = FragmentListAdapter.class.getSimpleName();
    private List<MomentItem> momentItemList;
    private Context context;
    private Store store = Store.getInstance();

    public MomentListAdapter(List<MomentItem> momentItemList, Context context) {
        this.momentItemList = momentItemList;
        this.context = context;
    }

    @Override
    public int getCount() {
        if (momentItemList == null) return 0;
        return momentItemList.size();
    }

    @Override
    public Object getItem(int i) {
        return momentItemList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            view = View.inflate(context, R.layout.moment_item, null);
            holder = new ViewHolder();
            holder.head = view.findViewById(R.id.head);
            holder.remark = view.findViewById(R.id.modify_context);
            holder.time = view.findViewById(R.id.time);
            holder.content = view.findViewById(R.id.content);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        MomentItem momentItem = momentItemList.get(i);
        if (momentItem.getHead_id().equals(store.getCurrentUser().getUsername())) {
            ImageUtil.glideLoad(context, momentItem.getHead_id(), store.cacheState, holder.head);
        } else {
            ImageUtil.glideLoad(context, momentItem.getHead_id(), 0, holder.head);
        }
        holder.remark.setText(momentItem.getRemark());
        holder.time.setText(DateUtil.getDate(momentItem.getTime()));
        holder.content.setText(momentItem.getContent());
        return view;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    public static class ViewHolder {
        public RoundImageView head;
        public TextView remark;
        public TextView time;
        public TextView content;
    }
}
